import { EventKeyboard, Input, input, KeyCode } from "cc";

/**
 * 按键管理
 */
export const keyMgr = _ as KeyMgr;
export class KeyMgr {

    private _keyDownDic = {};

    init() {
        (keyMgr as any) = this;

        input.on(Input.EventType.KEY_DOWN, e => {
            this._keyDownDic[e.keyCode] = true;
        });

        input.on(Input.EventType.KEY_UP, e => {
            delete this._keyDownDic[e.keyCode];
        });
    }

    /** 是否按下Alt键 */
    get isAlt() { return this.isKeyDown(KeyCode.ALT_LEFT) || this.isKeyDown(KeyCode.ALT_RIGHT); }
    /** 是否按下Ctrl键 */
    get isCtrl() { return this.isKeyDown(KeyCode.CTRL_LEFT) || this.isKeyDown(KeyCode.CTRL_RIGHT); }
    /** 是否按下Shift键 */
    get isShift() { return this.isKeyDown(KeyCode.SHIFT_LEFT) || this.isKeyDown(KeyCode.SHIFT_RIGHT); }

    /**
     * 按键是否正按下
     * @param key 
     */
    isKeyDown(key: KeyCode) {
        return !!this._keyDownDic[key];
    }

}